<?php

/**
 * Created by PhpStorm.
 * User: bob
 * Date: 21/9/25
 * Time: 上午10:37
 */

namespace App\Api\Web\Controllers;

use App\Api\Account\Models\UserCollection;
use App\Api\Web\Models\Bbs;
use App\Api\Web\Models\House;
use App\Api\Web\Models\HouseWanted;
use App\Api\Web\Services\BbsService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

// 收藏
class CollectionController extends Controller
{
    public function collect(Request $request, $source)
    {
        $params = $request->all();
        $user = authUser();
        $action = data_get($params, 'action', 1);
        $sourceId = data_get($params, 'source_id', '');
        if (empty($sourceId)) {
            throw new \LogicException('缺少必要参数', 100);
        }

        switch ($source) {
            case 'house':
                $type = 1;
                $m = \DB::table('house')->where('id', $sourceId)
                    ->first();
                if ($action != 1) {
                    if (data_get($m, 'collect_count') > 0) {
                        \DB::table('house')->where('id', $sourceId)->decrement('collect_count');
                    }
                } else {
                    \DB::table('house')->where('id', $sourceId)->increment('collect_count');
                }
                break;
            case 'house-wanted':
                $type = 2;
                $m = \DB::table('house_wanted')->where('id', $sourceId)
                    ->first();
                if ($action != 1) {
                    if (data_get($m, 'collect_count') > 0) {
                        \DB::table('house_wanted')->where('id', $sourceId)->decrement('collect_count');
                    }
                } else {
                    \DB::table('house_wanted')->where('id', $sourceId)->increment('collect_count');
                }
                break;
            case 'bbs':
                $type = 3;
                $m = \DB::table('bbs')->where('id', $sourceId)
                    ->first();
                if ($action != 1) {
                    if (data_get($m, 'collect_count') > 0) {
                        \DB::table('bbs')->where('id', $sourceId)->decrement('collect_count');
                    }
                } else {
                    \DB::table('bbs')->where('id', $sourceId)->increment('collect_count');
                }
                break;
            case 'house-bbs':
                $type = 4;
                $m = \DB::table('house')->where('id', $sourceId)
                    ->first();
                if ($action != 1) {
                    if (data_get($m, 'collect_count') > 0) {
                        \DB::table('house')->where('id', $sourceId)->decrement('collect_count');
                    }
                } else {
                    \DB::table('house')->where('id', $sourceId)->increment('collect_count');
                }
                break;
            case 'house-wanted-bbs':
                $type = 5;
                $m = \DB::table('house_wanted')->where('id', $sourceId)
                    ->first();
                if ($action != 1) {
                    if (data_get($m, 'collect_count') > 0) {
                        \DB::table('house_wanted')->where('id', $sourceId)->decrement('collect_count');
                    }
                } else {
                    \DB::table('house_wanted')->where('id', $sourceId)->increment('collect_count');
                }
                break;
            default:
                throw new NotFoundHttpException();
        }


        if ($action == 1) {
            $isCollect = UserCollection::where('type', '=', $type)
                ->where('user_id', '=', $user->id)
                ->where('source_id', '=', $sourceId)
                ->first();
            if (empty($isCollect)) {
                UserCollection::create([
                    'type' => $type,
                    'source_id' => $sourceId,
                    'user_id' => $user->id
                ]);
            }
        } elseif ($action == 2) {
            $isCollect = UserCollection::where('type', '=', $type)
                ->where('user_id', '=', $user->id)
                ->where('source_id', '=', $sourceId)
                ->delete();
        } else {
            throw new \LogicException('非法操作', 200);
        }

        return $this->formatOutput('success');


    }

    public function collectIndex(Request $request, $source)
    {
        $params = $request->all();
        $user = authUser();

        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);

        $m = UserCollection::where('user_collection.user_id', '=', $user->id);
        switch ($source) {
            case 'house':
                $type = 1;
                $m = $m->join('house as h', 'h.id', '=', 'user_collection.source_id')
                    ->where('h.status', '>', -900)
                    ->where('user_collection.type', '=', $type)
                    ->orderBy('h.id', 'desc')
                    ->paginate($pageSize, ['*'], 'page', $page)
                    ->toArray();

                foreach ($m['data'] as $k => $v) {
                    $m['data'][$k]['role_name'] = data_get(House::$role, data_get($v, 'role', ''), '未知');
                    $m['data'][$k]['rent_out_name'] = data_get(House::$rent_out, data_get($v, 'rent_out', ''), '未知');
                    $m['data'][$k]['toward_name'] = data_get(House::$toward, data_get($v, 'toward', ''), '未知');
                    $m['data'][$k]['facilities_name'] = data_get(House::$facilities, data_get($v, 'facilities', ''), '未知');
                    $m['data'][$k]['payment_account_name'] = data_get(House::$payment_account, data_get($v, 'payment_account', ''), '未知');
                    $m['data'][$k]['contract_name'] = data_get(House::$contract, data_get($v, 'contract'), '未知');
                    $m['data'][$k]['is_has_free_name'] = data_get(House::$is_has_free, data_get($v, 'is_has_free', ''), '未知');
                    $m['data'][$k]['is_check_name'] = data_get(House::$is_check, data_get($v, 'is_check', ''), '未知');
                    $m['data'][$k]['toilet_type_name'] = data_get(House::$toilet_type, data_get($v, 'toilet_type', ''), '未知');
                    $m['data'][$k]['bedroom_type_name'] = data_get(House::$bedroom_type, data_get($v, 'bedroom_type', ''), '未知');
                    $m['data'][$k]['sex_limit_name'] = data_get(House::$sex_limit, data_get($v, 'sex_limit', ''), '未知');
                    $m['data'][$k]['photos'] = json_decode(data_get($v, 'photos', ''));
                }

                return $this->formatOutput($m);
                break;
            case 'house-wanted':
                $type = 2;
                $m = $m->join('house_wanted as h', 'h.id', '=', 'user_collection.source_id')
                    ->leftJoin('users as u', 'u.id', '=', 'h.user_id')
                    ->where('h.status', '>', -900)
                    ->select('h.*', 'u.name', 'u.head_img')
                    ->where('user_collection.type', '=', $type)
                    ->orderBy('h.id', 'desc')
                    ->paginate($pageSize, ['*'], 'page', $page)
                    ->toArray();
                foreach ($m['data'] as $k => $v) {
                    $m['data'][$k]['rent_out'] = data_get(HouseWanted::$rent_out, data_get($v, 'rent_out', ''), '未知');
                    $m['data'][$k]['toilet_type'] = data_get(HouseWanted::$toilet_type, data_get($v, 'toilet_type', ''), '未知');
                    $m['data'][$k]['bedroom_type'] = data_get(HouseWanted::$bedroom_type, data_get($v, 'bedroom_type', ''), '未知');
                }

                return $this->formatOutput($m);
                break;
            case 'bbs':
                $type = 3;
                $m = $m->join('bbs as h', 'h.id', '=', 'user_collection.source_id')
                    ->where('h.status', '>', -900)
                    ->where('user_collection.type', '=', $type)
                    ->orderBy('h.id', 'desc')
                    ->paginate($pageSize, ['*'], 'page', $page)
                    ->toArray();

                return $this->formatOutput($m);
                break;
            case 'house-bbs':
                $type = 4;
                $m = $m->join('house as h', 'h.id', '=', 'user_collection.source_id')
                    ->where('h.status', '>', -900)
                    ->where('h.is_social', '=', 1)
                    ->where('user_collection.type', '=', $type)
                    ->orderBy('h.id', 'desc')
                    ->paginate($pageSize, ['*'], 'page', $page)
                    ->toArray();
                foreach ($m['data'] as $k => $v) {
                    $m['data'][$k]['role_name'] = data_get(House::$role, data_get($v, 'role', ''), '未知');
                    $m['data'][$k]['rent_out_name'] = data_get(House::$rent_out, data_get($v, 'rent_out', ''), '未知');
                    $m['data'][$k]['toward_name'] = data_get(House::$toward, data_get($v, 'toward', ''), '未知');
                    $m['data'][$k]['facilities_name'] = data_get(House::$facilities, data_get($v, 'facilities', ''), '未知');
                    $m['data'][$k]['payment_account_name'] = data_get(House::$payment_account, data_get($v, 'payment_account', ''), '未知');
                    $m['data'][$k]['contract_name'] = data_get(House::$contract, data_get($v, 'contract'), '未知');
                    $m['data'][$k]['is_has_free_name'] = data_get(House::$is_has_free, data_get($v, 'is_has_free', ''), '未知');
                    $m['data'][$k]['is_check_name'] = data_get(House::$is_check, data_get($v, 'is_check', ''), '未知');
                    $m['data'][$k]['toilet_type_name'] = data_get(House::$toilet_type, data_get($v, 'toilet_type', ''), '未知');
                    $m['data'][$k]['bedroom_type_name'] = data_get(House::$bedroom_type, data_get($v, 'bedroom_type', ''), '未知');
                    $m['data'][$k]['sex_limit_name'] = data_get(House::$sex_limit, data_get($v, 'sex_limit', ''), '未知');
                    $m['data'][$k]['photos'] = json_decode(data_get($v, 'photos', ''));
                }

                return $this->formatOutput($m);
                break;
            case 'house-wanted-bbs':
                $type = 5;
                $m = $m->join('house_wanted as h', 'h.id', '=', 'user_collection.source_id')
                    ->where('h.status', '>', -900)
                    ->where('h.is_social', '=', 1)
                    ->where('user_collection.type', '=', $type)
                    ->orderBy('h.id', 'desc')
                    ->paginate($pageSize, ['*'], 'page', $page)
                    ->toArray();
                foreach ($m['data'] as $k => $v) {
                    $m['data'][$k]['rent_out'] = data_get(HouseWanted::$rent_out, data_get($v, 'rent_out', ''), '未知');
                    $m['data'][$k]['toilet_type'] = data_get(HouseWanted::$toilet_type, data_get($v, 'toilet_type', ''), '未知');
                    $m['data'][$k]['bedroom_type'] = data_get(HouseWanted::$bedroom_type, data_get($v, 'bedroom_type', ''), '未知');
                }

                return $this->formatOutput($m);
                break;
            default:
                throw new NotFoundHttpException();
        }

    }

    public function isCollect(Request $request, $source)
    {
        $params = $request->all();
        $user = authUser();

        switch ($source) {
            case 'house':
                $type = 1;
                break;
            case 'house-wanted':
                $type = 2;
                break;
            case 'bbs':
                $type = 3;
                break;
            case 'house-bbs':
                $type = 4;
                break;
            case 'house-wanted-bbs':
                $type = 5;
                break;
            default:
                throw new NotFoundHttpException();
        }
        $sourceId = data_get($params, 'source_id', '');
        if (empty($sourceId)) {
            throw new \LogicException('缺少必要参数', 100);
        }

        $r = UserCollection::where('user_id', '=', $user->id)
            ->where('type', '=', $type)
            ->where('source_id', '=', $sourceId)
            ->first();

        $isCollect = 0;
        if (!empty($r)) {
            $isCollect = 1;
        }

        return $this->formatOutput([
            'is_collect' => $isCollect,
        ]);

    }

}
